<template>
    <a-card :bordered="false">
        <!-- 查询区域 -->
      <div class="table-page-search-wrapper">
        <a-form layout="inline" @keyup.enter.native="searchQuery">
          <a-row :gutter="24">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="任务档案编号">
                <j-input placeholder="请输入项目档案编号" v-model="queryParam.billNo"></j-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="项目名称">
                <a-input placeholder="请输入项目名称" v-model="queryParam.projectName"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="项目编号">
                <a-input placeholder="请输入项目编号" v-model="queryParam.finalNo"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="研发类型">
                <!-- <j-dict-select-tag placeholder="请选择研发类型" v-model="queryParam.auditStatus" dictCode="audit_status"/> -->
                <j-dict-select-tag placeholder="请选择研发类型" v-model="queryParam.developmentType" dictCode="development_type"/>

              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
             <a-form-item label="甲方单位">
                 <select-Company-input placeholder="请选择甲方单位" v-model="queryParam.signUnit" />
              </a-form-item>
            </a-col>
  
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
             <a-form-item label="乙方单位">
              <select-customer-input  commercialType="2" v-model="queryParam.supplierId" />

              <!-- <select-Company-input placeholder="请选择乙方单位" v-model="queryParam.supplierId" /> -->
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
             <a-form-item label="研发归属年度">
              <j-dict-select-tag placeholder="请选择研发归属年度" v-model="queryParam.year" dictCode="yjgsnd"/>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
             <a-form-item label="费用所属团队">
                <select-team-input
                    v-model="queryParam.teamName"
                    :tdlx="3"/>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
             <a-form-item label="研发团队">
              <select-team-input
                  v-model="queryParam.productTeam"
                  :tdlx="2"
                  :triggerChange="true"/>
              </a-form-item>
            </a-col>

            <a-col :xl="6" :lg="7" :md="8" :sm="24">
             <a-form-item label="研发部门">
              <j-select-depart placeholder="请选择研发部门"  v-model="queryParam.executeDepart"/>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
             <a-form-item label="研发负责人">
              <select-user-by-dep placeholder="请选择研发负责人" :multi="false" v-model="queryParam.projectLeading" />
              </a-form-item>
            </a-col>

            <!-- <a-col :xl="6" :lg="7" :md="8" :sm="24">
             <a-form-item label="流程状态">
                <j-dict-select-tag placeholder="请选择流程状态" v-model="queryParam.status" dictCode="project_process_tatus"/>
              </a-form-item>
            </a-col> -->
            <!-- <a-col :xl="6" :lg="7" :md="8" :sm="24">
             <a-form-item label="变更状态">
                <j-dict-select-tag placeholder="请选择变更状态" v-model="queryParam.changeStatus" dictCode="change_status"/>
              </a-form-item>
            </a-col> -->
            <!-- <a-col :xl="6" :lg="7" :md="8" :sm="24">
             <a-form-item label="项目档案状态">
                <j-dict-select-tag placeholder="请选择项目档案状态" v-model="queryParam.currentProjectStatus" dictCode="current_project_status"/>
              </a-form-item>
            </a-col> -->

            <a-col :xl="6" :lg="7" :md="8" :sm="24">
             <a-form-item label="业务分类">
              <business-category-name-select v-model="queryParam.businessCategoryName" @back="businessCategoryHandle"/>

                <!-- <j-dict-select-tag  type="list" v-model="queryParam.bussinessType" dictCode="ywfx_type" placeholder="请选择业务分类" /> -->
            </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
             <a-form-item label="任务状态">
              <j-dict-select-tag placeholder="请选择任务状态" v-model="queryParam.distributionType" dictCode="distribution_type"/>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
             <a-form-item label="结项状态">
                <j-dict-select-tag placeholder="请选择结项状态" v-model="queryParam.conclusionStatus" dictCode="conclusion_status"/>
              </a-form-item>
            </a-col>

            <a-col :xl="6" :lg="7" :md="8" :sm="24">
             <a-form-item label="结项时间">
              <a-range-picker
              :disabled="false"
              :allowClear="false"
              v-model="conclusionDate"
              style="width: 100%"
              @change="createTimeChange"/>
                <!-- <j-dict-select-tag placeholder="请选择结项时间" v-model="queryParam.auditStatus" dictCode="audit_status"/> -->
              </a-form-item>
            </a-col>
            <!-- <template v-if="toggleSearchStatus"></template> -->
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
                <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
                <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
                <a-button type="primary" icon="download" @click="handleExportXls('任务档案')" style="margin-left: 8px">导出</a-button>
                <!-- <a @click="handleToggleSearch" style="margin-left: 8px">
                  {{ toggleSearchStatus ? '收起' : '展开' }}
                  <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
                </a> -->
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <!-- 查询区域-END -->
  
      <!-- 操作按钮区域 -->
      <div class="table-operator">
        <!-- <a-button type="primary" icon="download" @click="handleExportXls('物料管理')" :disabled="isDisabledAuth('erpMaterial:export')">导出</a-button> -->
        <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" :before-upload="beforeUpload" @change="handleImportExcel">
          <a-button type="primary" icon="import" :disabled="isDisabledAuth('erpMaterial:import')">导入任务模板</a-button>
        </a-upload>
        <a-button type="primary" icon="download" @click="download" :disabled="isDisabledAuth('erpMaterial:export')">导出任务模板</a-button>
        <!-- 高级查询区域 -->
        <!--<j-super-query :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query>-->
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <a-menu slot="overlay">
            <a-menu-item key="1" @click="batchDel" :disabled="isDisabledAuth('erpMaterial:delete')"><a-icon type="delete"/>删除</a-menu-item>
            <a-menu-item key="2" @click="batchSubmit" :disabled="isDisabledAuth('erpMaterial:submit')"><a-icon type="to-top" />提交</a-menu-item>
          </a-menu>
          <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
        </a-dropdown>
        <a-popover title="自定义列" trigger="click" placement="leftBottom">
          <template slot="content">
            <a-checkbox-group @change="onColSettingsChange" v-model="settingColumns" :defaultValue="settingColumns">
              <a-row>
                <template v-for="(item,index) in defColumns">
                  <template v-if="item.key!='rowIndex'&& item.dataIndex!='action'">
                    <a-col :span="12" :key="index"><a-checkbox :value="item.dataIndex">{{ item.title }}</a-checkbox></a-col>
                  </template>
                </template>
              </a-row>
            </a-checkbox-group>
          </template>
          <a style="float: right;"><a-icon type="setting" :style='iconStyle'/></a>
        </a-popover>
      </div>
      <!-- table区域-begin -->
      <div>
        <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
          <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
          <a style="margin-left: 24px" @click="onClearSelected">清空</a>
        </div>
        <a-table
          ref="table"
          size="middle"
          bordered
          rowKey="id"
          :scroll="{x: true}"
          :components="draggableComponent"
          :columns="columns"
          :dataSource="dataSource"
          :pagination="ipagination"
          :loading="loading"
          :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
          class="j-table-force-nowrap"
          :customRow="customRow"
          @change="handleTableChange">
  
          <template slot="provision" slot-scope="text,record">
            <span>{{record.provisionBroad_dictText + '-' + record.provisionSmall_dictText}}</span>
          </template>
          <a slot="materialBill" :title="text" slot-scope="text, record" @click="getDetail(record)">{{ text }}</a>
          <template slot="status" slot-scope="status">
            <a-badge :status="status | statusTypeFilter" :text="status | statusFilter"/>
          </template>
          <template slot="htmlSlot" slot-scope="text">
            <div v-html="text"></div>
          </template>
          <!-- <template slot="imgSlot" slot-scope="text">
            <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
            <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
          </template> -->
          <template slot="fileSlot" slot-scope="text">
            <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
            <a-button
              v-else
              :ghost="true"
              type="primary"
              icon="download"
              size="small"
              @click="downloadFile(text)">
              下载
            </a-button>
          </template>
          <span slot="action" slot-scope="text, record">
          <template v-if="record.auditStatus==0">
              <a-button
                      class="ruleButton"
                      :ghost="true"
                      type="primary"
                      icon="edit"
                      @click="handleEdit(record)"
                      :disabled="isDisabledAuth('erpMaterial:edit')"
              />
              <a-divider type="vertical"/>
              <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a-button
                          class="ruleButton"
                          :ghost="true"
                          type="primary"
                          icon="delete"
                          :disabled="isDisabledAuth('erpMaterial:delete')"/>
              </a-popconfirm>
              <a-divider type="vertical"/>
              <a-popconfirm title="确定提交吗?" @confirm="() => handleSubmit(record.id)">
                  <a-button type="primary" size="small" :disabled="isDisabledAuth('erpMaterial:submit')">提交</a-button>
              </a-popconfirm>
          </template>
          </span>
        </a-table>
      </div>
  
      <!-- <erp-material-modal ref="modalForm" @ok="modalFormOk"></erp-material-modal> -->
      <!-- <audit-modal ref="auditModal" @ok="modalFormOk"></audit-modal> -->
    </a-card>
  </template>
  <script>
    import '@/assets/less/TableExpand.less'
    import { mixinDevice } from '@/utils/mixin'
    import SelectTeamInput from '@/views/scy/components/SelectTeamInput'
    import SelectCustomerInput from '@/views/scy/components/SelectCustomerInput'
    import SelectUserByDep from '@/views/scy/components/SelectUserByDep';

    import { JeecgListMixin } from '@/mixins/JeecgListMixin'
    import { getAction } from '@/api/manage'
    import {DisabledAuthFilterMixin} from '@/mixins/DisabledAuthFilterMixin'
    import { filterMultiDictText, initDictOptions } from '@/components/dict/JDictSelectUtil'
    import SelectCompanyInput from '@/views/scy/components/SelectCompanyInput'
    import BusinessCategoryNameSelect from '@views/erp/components/BusinessCategoryNameSelect'

    export default {
      name: 'TaskProfile',
      mixins:[JeecgListMixin, mixinDevice, DisabledAuthFilterMixin],
      components: {
        SelectCompanyInput,
        SelectUserByDep,
        SelectCustomerInput,
        BusinessCategoryNameSelect,
        SelectTeamInput
      },
      data () {
        return {
          description: '物料管理管理页面',
          //表头
          columns:[],
          //列设置
          settingColumns:[],
          // 列定义
          defColumns: [
            {
              title: '序号',
              dataIndex: '',
              key:'rowIndex',
              width:60,
              align:"center",
              customRender:function (t,r,index) {
                return parseInt(index)+1;
              }
            },
            {
              title:'任务档案编号',
              align:"center",
              width:100,
              dataIndex: 'billNo',
              scopedSlots: { customRender: 'materialBill' }
            },
            {
              title:'项目名称',
              align:"center",
              width:300,
              dataIndex: 'projectName',
            },
            {
              title:'项目编号',
              align:"center",
              width:100,
              dataIndex: 'finalNo',
            },
            {
              title:'研发类型',
              align:"center",
              width:200,
              dataIndex: 'developmentType_dictText',
            },

            {
              title:'甲方单位',
              align:"center",
              width:150,
              dataIndex: 'signUnit_dictText',
            },
            {
              title:'乙方单位',
              align:"center",
              width:200,
              dataIndex: 'supplierId_dictText',
            },
            {
              title:'研发归属年度',
              align:"center",
              dataIndex: 'year',
              width:147
            },
            {
              title:'费用所属团队',
              align:"center",
              dataIndex: 'teamName_dictText',
              width:147
            },
            {
              title:'业务分类',
              align:"center",
              dataIndex: 'categoryName',
              width:147
            },
            {
              title:'研发团队',
              align:"center",
              dataIndex: 'productTeam_dictText',
            },
            {
              title:'研发部门',
              align:"center",
              dataIndex: 'executeDepart_dictText',
            },
            {
              title:'研发负责人',
              align:"center",
              dataIndex: 'projLeadName',
            },
            {
              title:'任务进度(%)',
              align:"center",
              dataIndex: 'taskProgress',
            },
            {
              title:'变更状态',
              align:"center",
              dataIndex: 'changeStatus_dictText',
            },
            {
              title:'任务状态',
              align:"center",
              dataIndex: 'distributionType_dictText',
            },
            {
              title:'结项状态',
              align:"center",
              dataIndex: 'conclusionStatus_dictText',
            },
            {
              title:'结项时间',
              align:"center",
              dataIndex: 'conclusionDate',
              // width:147
            },

          ],
          url: {
            staticUrl:"api/erpmng/sys/common/static/",
            // list: "/api/erp/purchase/material/erpMaterial/list",
            list: "/api/erp/omc/project/erpRAndDOmcProjectTaskFinal/queryPageList",
            delete: "/api/erp/purchase/material/erpMaterial/delete",
            deleteBatch: "/api/erp/purchase/material/erpMaterial/deleteBatch",
            // exportXlsUrl: "/api/erp/purchase/material/erpMaterial/exportXlsTemplate",
            exportXlsUrl: "/api/erp/omc/project/erpRAndDOmcProjectTaskFinal/exportXls",
            exportXlsTemplate: "/api/erp/purchase/material/erpMaterial/exportXlsTemplate",
            // importExcelUrl: "api/erp/purchase/material/erpMaterial/importExcel",
            importExcelUrl: "api/erp/omc/project/erpRAndDOmcProjectTask/importExcel",
            submit: "/api/erp/purchase/material/erpMaterial/submit",
            submitBatch: "/api/erp/purchase/material/erpMaterial/submitBatch",
            revoke: "/api/erp/purchase/material/erpMaterial/revoke",
            revokeBatch: "/api/erp/purchase/material/erpMaterial/revokeBatch",
            counterTrial: "/api/erp/purchase/material/erpMaterial/counterTrial",
            counterTrialBatch: "/api/erp/purchase/material/erpMaterial/counterTrialBatch",
            audit: "/api/erp/purchase/material/erpMaterial/audit",
            auditBatch: "/api/erp/purchase/material/erpMaterial/auditBatch",
            overrule: "/api/erp/purchase/material/erpMaterial/overrule",
            overruleBatch: "/api/erp/purchase/material/erpMaterial/overruleBatch",
            queryProvisionBroadList: "/api/erp/purchase/material/erpMaterialCategory/queryAllParent",
            queryProvisionSmallList: "/api/erp/purchase/material/erpMaterialCategory/queryChildrenByParentId"
          },
          dictOptions:{},
          superFieldList:[],
          conclusionDate:[],
          provisionBroadList: [],
          provisionSmallList: [],
          visiblelist: false,
          attachmentList: null
        }
      },
      created() {
        this.getSuperFieldList();
        this.findProvisionBroadList()
      },
      computed: {
        importExcelUrl: function(){
          return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
        },
      },
      methods: {

        beforeUpload(file){
             const isExcel = [
              'application/vnd.ms-excel',
              'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
            ].includes(file.type);

            const isExtensionValid = ['.xls', '.xlsx'].some(ext => 
              file.name.toLowerCase().endsWith(ext)
            );

            if (!isExcel || !isExtensionValid) {
               this.$message.error('只能导入Excel文件（.xls 或 .xlsx）!');
               return false
            }
            
        },

        createTimeChange(date, dateString) {
          this.$set(this.queryParam, 'conclusionDateBegin', dateString[0] + ' 00:00:00')
          this.$set(this.queryParam, 'conclusionDateEnd', dateString[1] + ' 23:59:59')
         },
        download() {
            initDictOptions('development_task_download').then((res) => {
              if (res.success) {
                let data = res.result
                location.href = `${window._CONFIG['domianURL']}/${this.url.staticUrl}`+data[0].value
              }
            })
      },

       businessCategoryHandle(obj) {
            if (obj) {
              this.queryParam.businessCategory = obj.categoryCode;
            }
          },
        
                //查看详情
         getDetail(record) {
          console.log(record,'------')
          this.$router.push({
            // src\views\erp\devProject\taskManage\TaskProfileDetail\index.vue
             path: `/erp/devProject/taskManage/TaskProfileDetail/index/${record.id}`
            })
        },

        handleDetail: function (record) {
      const _path = this.$route.path
      // console.log('path========', _path)

      //debugger;
      if (_path.startsWith('/hmhb/') || _path.startsWith('/scy/') || _path.startsWith('/imp/')
        || _path.startsWith('/erp/')) {
        this.$router.push({
          path: `/erp/devProject/taskManage/TaskProfileDetail/index/${record.id}`
        })
      } else {
        this.$refs.modalForm.edit(record);
        this.$refs.modalForm.title = '详情';
        this.$refs.modalForm.disableSubmit = true;
      }
      //   console.log(this.$route)
    },

        searchReset() {
          this.queryParam = {}
          this.provisionSmallList = []
          this.conclusionDate = []
          this.loadData(1)
        },
        handlelistOk(){
          this.visiblelist = false;
        },
        findAttachment(item) {
          this.attachmentList = item.attachment
          this.visiblelist = true
        },
        handleChange(value) {
          const that = this
          this.provisionSmallList = []
          this.$set(this.queryParam, 'provisionSmall', undefined);
          if (!!value) {
            const parentId = value
            getAction(this.url.queryProvisionSmallList, { parentId }).then(res => {
              if (res.success) {
                res.result.forEach((res) => {
                  that.provisionSmallList.push({ 'id':res.id, 'categoryName':res.categoryName })
                })
              } else {
                that.$message.warning(res.message)
              }
            })
          }
        },
        filterOption(input, option) {
          return (
            option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
          );
        },
        findProvisionBroadList() {
          const that = this
          that.provisionBroadList = []
          getAction(this.url.queryProvisionBroadList).then(res => {
            if (res.success) {
              res.result.forEach((res) => {
                that.provisionBroadList.push({ 'id':res.id, 'categoryName':res.categoryName })
              })
            } else {
              that.$message.warning(res.message)
            }
          })
        },
        initDictConfig(){
        },
        getSuperFieldList(){
          let fieldList=[];
          fieldList.push({type:'string',value:'materialBill',text:'物料编码',dictCode:''})
          fieldList.push({type:'string',value:'provisionBroad',text:'供应大类',dictCode:''})
          fieldList.push({type:'string',value:'provisionSmall',text:'供应小类',dictCode:''})
          fieldList.push({type:'string',value:'materialName',text:'物料名称',dictCode:''})
          fieldList.push({type:'string',value:'specification',text:'规格型号',dictCode:''})
          fieldList.push({type:'string',value:'brand',text:'品牌',dictCode:''})
          fieldList.push({type:'string',value:'manufacturer',text:'制造商',dictCode:''})
          fieldList.push({type:'string',value:'production',text:'产地',dictCode:''})
          fieldList.push({type:'BigDecimal',value:'referencePrice',text:'参考价格',dictCode:''})
          fieldList.push({type:'string',value:'unit',text:'单位',dictCode:''})
          fieldList.push({type:'string',value:'ohterOne',text:'其他属性1',dictCode:''})
          fieldList.push({type:'string',value:'ohterTwo',text:'其他属性2',dictCode:''})
          fieldList.push({type:'string',value:'remark',text:'备注',dictCode:''})
          fieldList.push({type:'int',value:'auditStatus',text:'审核状态',dictCode:''})
          this.superFieldList = fieldList
        }
      }
    }
  </script>
  <style scoped>
    @import '~@assets/less/common.less';
  </style>
  